//
//  PCUserRegisterViewController.m
//  PhotoCircle
//
//  Created by GL on 15/12/10.
//  Copyright © 2015年 wujungao. All rights reserved.
//

#import "PCUserRegisterViewController.h"
#import "PCUserViewController.h"
#import "CocoaSecurity.h"
#import "PCMeStore.h"
#import "GMString.h"
#import "GMTipsHelper.h"

#define TimeIntervalToCaptcha       59.0

@interface PCUserRegisterViewController ()
@property (weak, nonatomic) IBOutlet UILabel *registerLabel;

@property (weak, nonatomic) IBOutlet UITextField *phoneTextFile;
@property (weak, nonatomic) IBOutlet UITextField *captchaTextFile;
@property (weak, nonatomic) IBOutlet UITextField *passWordTextField;

@property (weak, nonatomic) IBOutlet UIButton *registerButton;
@property (weak, nonatomic) IBOutlet UIButton *captchaButton;
@property (weak, nonatomic) IBOutlet UILabel *captchaTipLabel;

@property (nonatomic, assign) NSInteger captchaInterval;//验证码间隔
@property (nonatomic, strong) NSTimer *captchaTimer;//验证码计时器

@end

@implementation PCUserRegisterViewController

-(void)configUI
{
    _registerButton.layer.cornerRadius = 3;
    _registerButton.layer.masksToBounds = YES;
    
    _captchaButton.layer.cornerRadius = 3;
    _captchaButton.layer.masksToBounds = YES;

    _registerLabel.text = self.type;
    self.captchaInterval = TimeIntervalToCaptcha;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configUI];
    // Do any additional setup after loading the view from its nib.
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    MCHiddenBottomView();
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)registerPopViewController
{
    NSArray *navarray=[self.navigationController childViewControllers];
    UIViewController *popViewController;
    for (UIViewController *pVC in navarray) {
        if ([pVC isKindOfClass:[PCUserViewController class]]) {
            popViewController = pVC;
        }
    }
    [self.navigationController popToViewController:popViewController animated:YES];
}

#pragma mark -
#pragma mark -Button Event
- (IBAction)backEvent:(UIButton *)sender {
    if (self.captchaTimer) {
        [self.captchaTimer invalidate];
        self.captchaTimer = nil;
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)registerEvent:(UIButton *)sender {
    
    if ([NSString isEmptyString:self.phoneTextFile.text]) {
        return;
    }
    
    if ([NSString isEmptyString:self.captchaTextFile.text]) {
        return;
    }
    
    if ([NSString isEmptyString:self.passWordTextField.text]) {
        return;
    }

    [GMTipsHelper showLoading];
    if ([self.type isEqual:@"修改密码"]) {
        [self resetPassWord];
    }else{
        [self registerNumber];
    }
}

// 修改密码
-(void)resetPassWord
{
    NSString *md5PassWord =[CocoaSecurity md5:self.passWordTextField.text].hexLower;
    NSDictionary *pDic = @{@"phone":self.phoneTextFile.text,@"password":md5PassWord,@"captcha":self.captchaTextFile.text};
    NSLog(@"pDic :%@",pDic);
    
    [[PCMeStore sharedMeStore] requestResetPassWord:pDic successHandler:^(NSInteger responCode){
        
        // 存储userInfo
        if (responCode == API_RESPONSE_OK) {
            
            [GMTipsHelper showSuccess:[NSString stringWithFormat:@"%@成功",self.type]];
            [[PCUserInfoManager sharedUserInfo] cleanLocalUserInfo];
            [[PCUserInfoManager sharedUserInfo] saveUserInfo:[PCMeStore sharedMeStore].userModel];
            [self performSelector:@selector(registerPopViewController) withObject:nil afterDelay:0.8];;
        }else{
            [GMTipsHelper showFail:[NSString stringWithFormat:@"%@失败",self.type]];
        }

    } errorHandler:^{
        // 注册失败；
        [GMTipsHelper showBadNetwork];
    }];
}

// 用户注册
-(void)registerNumber
{
    NSString *md5PassWord =[CocoaSecurity md5:self.passWordTextField.text].hexLower;
    NSDictionary *pDic = @{@"phone":self.phoneTextFile.text,@"password":md5PassWord,@"captcha":self.captchaTextFile.text};
    NSLog(@"pDic :%@",pDic);
    [[PCMeStore sharedMeStore] requestUserRegister:pDic successHandler:^(NSInteger responCode){
        // 存储userInfo
        if (responCode == API_RESPONSE_OK) {
            
            [GMTipsHelper showSuccess:[NSString stringWithFormat:@"%@成功",self.type]];
            [[PCUserInfoManager sharedUserInfo] saveUserInfo:[PCMeStore sharedMeStore].userModel];
            [self performSelector:@selector(registerPopViewController) withObject:nil afterDelay:0.8];;
        }else{
            [GMTipsHelper showFail:[NSString stringWithFormat:@"%@失败",self.type]];
        }
    } errorHandler:^{
        // 注册失败；
        [GMTipsHelper showBadNetwork];
    }];
}

- (IBAction)captchaEvent:(UIButton *)sender {
    
    if ([NSString isEmptyString:self.phoneTextFile.text]) {
        return;
    }
    
    self.captchaTipLabel.text = [NSString stringWithFormat:@"%02ld%@", (long)self.captchaInterval,@"秒后重发"];
    self.captchaButton.enabled = NO;
    
    // 添加定时
    self.captchaTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(captchaIntervalChange:) userInfo:nil repeats:YES];
    //[self resendVerificationCode];
    
    NSDictionary *pDic = @{@"phone":self.phoneTextFile.text};
    [GMTipsHelper showLoading];
    [[PCMeStore sharedMeStore] requestUserCaptcha:pDic successHandler:^(NSInteger responCode){
        if (responCode == API_RESPONSE_OK) {
            [GMTipsHelper showSuccess:@"获取验证码成功"];
        }else{
            [GMTipsHelper showFail:@"获取验证码失败"];
        }
    } errorHandler:^{
        [GMTipsHelper showBadNetwork];
    }];
}

-(void)captchaIntervalChange:(NSTimer*)timer
{
    
    self.captchaInterval--;
    if (self.captchaInterval != 0) {
        self.captchaTipLabel.text = [NSString stringWithFormat:@"%02ld%@", (long)self.captchaInterval,@"秒后重发"];
    }else if(self.captchaInterval ==0) {
        //[self.lable_forgetPassWord setBackgroundColor:PasswordUnSelectColor];
        self.captchaInterval = TimeIntervalToCaptcha;
        self.captchaTipLabel.text = @"获取验证码";
        self.captchaButton.enabled = YES;
        [self.captchaTimer invalidate];
        self.captchaTimer = nil;
    }
}

@end
